package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CommonDeleteMapper;
import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.dao.SupplierDao;
import cn.gjculture.shopping.api.entity.Supplier;
import cn.gjculture.shopping.api.service.SupplierService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    SupplierDao supplierDao;

    @Autowired
    CommonDeleteMapper commonDeleteMapper;
    @Autowired
    CommonUpdateMapper commonUpdateMapper;

    @Override
    public int insert(Supplier supplier) {
        supplier.setSupplierId(UUIDUtil.getUUID());
        supplier.setUpdateTime(DateUtil.getTimestamp());
        return supplierDao.insert(supplier);
    }

    @Override
    public int update(Supplier supplier) {
        supplier.setUpdateTime(DateUtil.getTimestamp());
        return commonUpdateMapper.updateSupplier(supplier);
    }

    @Override
    public List<Supplier> query() {
        return supplierDao.query();
    }

    @Override
    public Supplier queryById(String supplierId) {
        return supplierDao.queryById(supplierId);
    }

    @Override
    public int delete(List<String> supplierIds) {
        return commonDeleteMapper.deleteSupplier(supplierIds);
    }

    @Override
    public List<Supplier> queryForRoutingRule() {
        List<Supplier> supplierList=new ArrayList<>();
        List<Supplier> suppliers=supplierDao.queryForRoutingRule();
        if(!CollectionUtils.isEmpty(suppliers)){
            for(Supplier s:suppliers){
                if(StringUtils.isEmpty(s.getSonMerNo())){
                    continue;
                }
                supplierList.add(s);
            }
        }
        return supplierList;
    }
}
